package com.meihua.code.linked;


import com.meihua.code.domain.SingleLinkedList;

import java.time.chrono.MinguoChronology;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
import java.util.concurrent.TransferQueue;

/**
 * @program: code
 * @description:
 * @author: meihua
 * @created: 2021/05/24 16:00
 */
public class Linked {

    /***
     * 反转单链表  使用栈
     **/
    public static   SingleLinkedList reversalLinked(SingleLinkedList listNode){

        Stack<Integer> stack= new Stack<>();

        while (listNode!=null){
            stack.push(listNode.val);
            listNode=listNode.next;
        }
        Integer pop = stack.pop();
        SingleLinkedList reversaList= new SingleLinkedList();
        reversaList.val=pop;
        SingleLinkedList temp= reversaList;
        while (!stack.isEmpty()){
            SingleLinkedList singleLinkedList = new SingleLinkedList(stack.pop());
            temp.next=singleLinkedList;
            temp=temp.next;
        }
        return reversaList;
    }

    /***
     * 反转单链表
     **/
    public static   SingleLinkedList reversalLinked2(SingleLinkedList head){
        SingleLinkedList pre= null;
        SingleLinkedList next= null;

        while (head!=null){
            next=head.next;
            head.next=pre;
            pre=head;
            head=next;
        }
        return pre;
    }


    public static void main(String[] args) {
        SingleLinkedList singleLinkedList = SingleLinkedList.getSingleLinkedList(new int[]{1, 2, 3,4});
      System.out.println(singleLinkedList);

//        System.out.println(is(new int[]{1,2,4,5,6,7,4}));
        System.out.println(reversalLinked2(singleLinkedList));

       //  System.out.println(getNum(new int[]{1, 2, 38, 3, 8,5}));
    }



    public  static  int getNum(int [] nums){
        int min=1;
        Set<Integer> set =new HashSet<>(nums.length);
        for (int num:nums ){
            if (!set.contains(num)&&num>0){
                min=Math.min(num,min);
            }
            //添加成功 判断是否相等 如果相等 自增
            if (set.add(num)&&min==num){
                min= getMin(set,min);
            }
        }
        return min;
    }

    public static int getMin(Set<Integer> set, int min){
        System.out.println("执行了");
       return !set.contains(min)?min:getMin(set,++min);
    }

    public static int is(int arr []){


        for(int i =0 ; i<arr.length-1 ; i++) {
            for(int j=0 ; j<arr.length-1 ; j++) {
                if(arr[i]==arr[j+1]) {
                   return arr[i];
                }
            }
        }
      return -1;
    }








}
